
package com.yck12;

import com.opencsv.CSVWriter;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;


public class Main1 {
    // 目标网页URL
    private static final String URL = "https://zhnfmdxzz.yiigle.com/aboutUs/490292.jhtml";

    // 常见中文姓氏，用于辅助识别人名
    private static final String[] FAMILY_NAMES = {
            "赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨",
            "朱", "秦", "尤", "许", "何", "吕", "施", "张", "孔", "曹", "严", "华", "金", "魏", "陶", "姜",
            "戚", "谢", "邹", "喻", "柏", "水", "窦", "章", "云", "苏", "潘", "葛", "奚", "范", "彭", "郎",
            "鲁", "韦", "昌", "马", "苗", "凤", "花", "方", "俞", "任", "袁", "柳", "酆", "鲍", "史", "唐",
            "费", "廉", "岑", "薛", "雷", "贺", "倪", "汤", "滕", "殷", "罗", "毕", "郝", "邬", "安", "常",
            "乐", "于", "时", "傅", "皮", "卞", "齐", "康", "伍", "余", "元", "卜", "顾", "孟", "平", "黄",
            "和", "穆", "萧", "尹", "姚", "邵", "湛", "汪", "祁", "毛", "禹", "狄", "米", "贝", "明", "臧",
            "计", "伏", "成", "戴", "谈", "宋", "茅", "庞", "熊", "纪", "舒", "屈", "项", "祝", "董", "梁"
    };

//    public static void main(String[] args) {
//        try {
//            // 1. 爬取网页内容
//            Document document = Jsoup.connect(URL)
//                    .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
//                    .timeout(10000)
//                    .get();
//
//            // 2. 提取可能包含人名的文本内容
//            // 这里根据网页结构，选择可能包含人名的元素
//            Elements potentialElements = document.select("div, p, span, li");
//
//            // 3. 识别人名
//            Set<String> personNames = extractPersonNames(potentialElements);
//
//            // 4. 生成CSV文件
//            generateCSV(personNames, "person_names.csv");
//
//            System.out.println("成功提取 " + personNames.size() + " 个人名，并已保存到 person_names.csv 文件中");
//
//        } catch (IOException e) {
//            System.err.println("爬取网页时发生错误: " + e.getMessage());
//            e.printStackTrace();
//        }
//    }
public static void main(String[] args) {
    String searchTerm = "COVERAGE[FullMatch]\"min zhu\"";
    String apiUrl = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi";

    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
        // 构建请求URL
        String requestUrl = String.format("%s?db=pubmed&term=%s&retmode=json",
                apiUrl,
                java.net.URLEncoder.encode(searchTerm, "UTF-8"));

        HttpGet request = new HttpGet(requestUrl);

        // 执行请求
        try (CloseableHttpResponse response = httpClient.execute(request)) {
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                String result = EntityUtils.toString(entity);
                System.out.println(result);
                // 解析JSON响应
                ObjectMapper mapper = new ObjectMapper();
                JsonNode rootNode = mapper.readTree(result);

                // 提取PMID列表
                JsonNode idListNode = rootNode.path("esearchresult").path("idlist");
                if (idListNode.isArray()) {
                    System.out.println("找到的文献PMID列表:");
                    for (JsonNode idNode : idListNode) {
                        System.out.println(idNode.asText());
                    }
                } else {
                    System.out.println("未找到匹配的文献");
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
    /**
     * 从HTML元素中提取人名
     */
    private static Set<String> extractPersonNames(Elements elements) {
        Set<String> names = new HashSet<>();

        for (Element element : elements) {
            String text = element.text().trim();
            if (text.isEmpty()) continue;

            // 简单的人名识别逻辑：基于中文姓氏和长度判断
            String[] words = text.split("[^\\u4e00-\\u9fa5]"); // 按非中文字符分割

            for (String word : words) {
                if (isPossibleName(word)) {
                    names.add(word);
                }
            }
        }

        return names;
    }

    /**
     * 判断字符串是否可能是人名
     */
    private static boolean isPossibleName(String word) {
        // 人名通常是2-4个汉字
        if (word.length() < 2 || word.length() > 4) {
            return false;
        }

        // 检查是否以常见姓氏开头
        String firstChar = word.substring(0, 1);
        for (String familyName : FAMILY_NAMES) {
            if (familyName.equals(firstChar)) {
                return true;
            }
        }

        return false;
    }

    /**
     * 生成CSV文件
     */
    private static void generateCSV(Set<String> names, String fileName) throws IOException {
        try (CSVWriter writer = new CSVWriter(new FileWriter(fileName))) {
            // 写入表头
            writer.writeNext(new String[]{"人名"});

            // 写入数据
            for (String name : names) {
                writer.writeNext(new String[]{name});
            }
        }
    }
}
